/*
 *  Licensed Materials - Property of IBM
 *  5725-G92 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
 *  US Government Users Restricted Rights - Use, duplication or
 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/**
 *  WL.Server.invokeHttp(parameters) accepts the following json object as an argument:
 *  
 *  {
 *  	// Mandatory 
 *  	method : 'get' , 'post', 'delete' , 'put' or 'head' 
 *  	path: value,
 *  	
 *  	// Optional 
 *  	returnedContentType: any known mime-type or one of "json", "css", "csv", "javascript", "plain", "xml", "html"  
 *  	returnedContentEncoding : 'encoding', 
 *  	parameters: {name1: value1, ... }, 
 *  	headers: {name1: value1, ... }, 
 *  	cookies: {name1: value1, ... }, 
 *  	body: { 
 *  		contentType: 'text/xml; charset=utf-8' or similar value, 
 *  		content: stringValue 
 *  	}, 
 *  	transformation: { 
 *  		type: 'default', or 'xslFile', 
 *  		xslFile: fileName 
 *  	} 
 *  } 
 */

/**
 * @param interest
 *            must be one of the following: world, africa, sport, technology, ...
 *            (The list can be found in http://edition.cnn.com/services/rss/)
 * @returns json list of items
 */
function getAllIssues() {
	
	//var token = getToken();
	var input = {
	    method : 'get',
	    returnedContentType : 'json',
	    path : '/issues.json',
	/*    headers: {
	    	'X-Redmine-API-Key' : 'beb3df095de55f6f1d3ec66514aa96819687b047'
	    }*/
	};
	
	return WL.Server.invokeHttp(input);
	//var response = JSON.stringify(WL.Server.invokeHttp(input));
	/* var result = WL.Server.invokeHttp(input);
	 var response = new Object();
	 response.isSuccessful = result.isSuccessful ;
	 response.issues = result.issues;
	 return response;*/
}
/**
 * 
 * @param interest
 *            must be one of the following: world, africa, sport, technology, ...
 *            (The list can be found in http://edition.cnn.com/services/rss/)
 * @returns json list of items
 */

function getApiKey(){
	var activeUser = JSON.parse(JSON.stringify(WL.Server.getActiveUser("CustomAuthenticatorRealm")));

	var token = activeUser.attributes.authenticationKey;
	//var token=activeUser.credentials;
//	var s = activeUser.credentials;
	//var s = "123";
/*	return {
		secretData: token
	}; */
	return token;
}

function getProjectInfo(project_id){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/projects/'+ project_id + '.json',
		    headers: {'X-Redmine-API-Key': getApiKey() }
		};
		
		
		return WL.Server.invokeHttp(input);
}



function getProjects() {
	
	var input = {
	    method : 'get',
	    returnedContentType : 'json',
	    path : '/projects.json',
	    headers: {"X-Redmine-API-Key": getApiKey()},
	};
	
	return WL.Server.invokeHttp(input);
}

function getProjectMemberships(project_id) {
	
	var input = {
	    method : 'get',
	    returnedContentType : 'json',
	    path : '/projects/' + project_id + '/memberships.json',
	    headers: {"X-Redmine-API-Key": getApiKey()}
	};
	
	return WL.Server.invokeHttp(input);	
}

function getProjectIssues(project_id) {
		
	var input = {
	    method : 'get',
	    returnedContentType : 'json',
	    path : '/issues.json?project_id=' + project_id,
	    headers: {"X-Redmine-API-Key": getApiKey()}
	};
	
	return WL.Server.invokeHttp(input);
}

function getIssue(issue_id) {
	
	var input = {
	    method : 'get',
	    returnedContentType : 'json',
	    path : '/issues/' + issue_id + ".json",
	    headers: {"X-Redmine-API-Key": getApiKey()}
	};
	
	
	return WL.Server.invokeHttp(input);
}

function getIssueStatuses(){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/issue_statuses.json',
		    headers: {"X-Redmine-API-Key": getApiKey()}
		};
		
		
		return WL.Server.invokeHttp(input);	
}

function getTimeEntryActivities(){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/enumerations/time_entry_activities.json',
		    headers: {"X-Redmine-API-Key": getApiKey()}
		};
		
		
		return WL.Server.invokeHttp(input);	
}
function getTrackers(){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/trackers.json',
		    headers: {"X-Redmine-API-Key": getApiKey()}
		};
		
		
		return WL.Server.invokeHttp(input);	
}

function getIssuePriorities(){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : "/enumerations/issue_priorities.json",
		    headers: {"X-Redmine-API-Key": getApiKey()}
		};
		
		
		return WL.Server.invokeHttp(input);	
}

function getProjectVersions(project_id){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : "/projects/"+project_id+"/versions.json",
		    headers: {"X-Redmine-API-Key": getApiKey()}
		};
		
		
		return WL.Server.invokeHttp(input);	
}


/**
 * 
 * @param issue_id
 * @returns
 */
function getIssueFormEnum(project_id) {
	var issueStatuses= getIssueStatuses();
	var issuePriorities= getIssuePriorities();
	var trackers = getTrackers();
	var projectVersions = getProjectVersions(project_id);
	var response = {};

	if (issueStatuses.isSuccessful && issuePriorities.isSuccessful && trackers.isSuccessful){
		if ( (issueStatuses.statusCode==200) && (issuePriorities.statusCode==200) && (trackers.statusCode==200)){
			
			response.isSuccessful = true;
			response.statusCode = 200;
		//	WL.Logger.error(JSON.stringify(issueStatuses.issue_statuses));
		//	WL.Logger.error(JSON.stringify(trackers.trackers));
		//	WL.Logger.error(JSON.stringify(issuePriorities.issue_priorities));
		//	WL.Logger.error(JSON.stringify(projectVersions.versions));
			response.issue_statuses = issueStatuses.issue_statuses ;
			response.trackers = trackers.trackers;
			response.issue_priorities = issuePriorities.issue_priorities;
			response.versions = projectVersions.versions;
			
			return response;
		}
	}
	else{
		
		response.isSuccessful = false;
		return response;
	}
	
}

function createIssue(json_data) {
	
	var input = {
	    method : 'post',
	    returnedContentType : 'json',
	    path : '/issues.json',
	
	    headers: {"X-Redmine-API-Key": getApiKey() },
	    body: {	
	    	contentType : "application/json" ,
	    	content : 	json_data 
	    	}
	};
	
	return WL.Server.invokeHttp(input);
}

function updateIssue(issue_id, json_data) {
	
	var input = {
	    method : 'put',
	    returnedContentType : 'json',
	    path : '/issues/' + issue_id + '.json',
	    data : json_data,
	    headers: {"X-Redmine-API-Key": getApiKey()}
	};
	
	return WL.Server.invokeHttp(input);
}

function getCurrentUser (){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/users/current.json?include=memberships',
		    headers: {"X-Redmine-API-Key":"123456789"}
		};
		
		return WL.Server.invokeHttp(input);	
}

function getRolePermissions(role_id){
	var input = {
		    method : 'get',
		    returnedContentType : 'json',
		    path : '/roles/'+role_id +".json",
		    headers: {"X-Redmine-API-Key": "123456789"}
		};
	return WL.Server.invokeHttp(input);	
}

function getUserPermissions(project_id){
	var current_user = getCurrentUser().user;
	WL.Logger.error(JSON.stringify(current_user));
	var membership = current_user.memberships.filter(function (membership) {return membership.project.id == project_id ;});
	var roles = membership.roles ;
	for (var i=0,len = roles.length; i<len; i ++){
		roles[i].permissions =  getRolePermissons(roles[i].id).role.permissions;
	}
	return roles;
	
}